//引入路由
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//1 定义路由
import Home from '@/views/Home.vue'
import Zhuanti from '@/views/Zhuanti.vue'
import Fenlei from '@/views/Fenlei.vue'
import Car from '@/views/Car.vue'
import My from '@/views/My.vue'
import Seach from '@/views/Seach.vue'
import Xiangqing from '@/components/Xiangqing.vue'
import Record from '@/components/Record.vue'
import Fen from '@/components/Fen.vue'
//2 定义路由
const routes = [
  {
    path: '/', redirect: '/home',
  },
  {
    path: '/home', component: Home,
    meta: { isshow: true },
    children: [{
      path: 'seach', component: Seach,
      meta: { isshow: false }
    }]
  },
  {
    path: '/zhuanti', component: Zhuanti,
    meta: { isshow: true }
  },
  {
    path: '/fenlei', component: Fenlei,
    meta: { isshow: true }
  },
  {
    path: '/car', component: Car,
    meta: { isshow: true }
  },
  {
    path: '/my', component: My,
    meta: { isshow: true }
  },
  {
    path: '/xiangqing', component: Xiangqing,
    meta: { isshow: false }
  },
  {
    path: '/record', component: Record,
    meta: { isshow: false },
   
  },
  {
    path: '/fen', component: Fen,
    meta: { isshow: false }
  }
]
// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};

//3 创建 router 实例
const router = new VueRouter({
  mode: 'history',
  routes // (缩写) 相当于 routes: routes
})

router.beforeEach((to, from, next) => {
  next()
  if (to.path == '/car') {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      Vue.prototype.$toast({
        type: "fail",
        duration: "1000",
        message: "请先登录",
      });
      next('/my')
    }
  } else {
    next()
  }
})
export default router;
